//index.js
//获取应用实例
const app = getApp()
const Api = require("../../utils/api.js").API
// const Store = require("../../utils/store.js").store
const codeZ = require("../../utils/util.js")
Page({
    data: {
        list: [],
        goods: {},
        value: '为他/她添加一个祝福，表达您的心意...',
        valueImage: [],
        blNo: '',
        currentDate: '请选择配送时间',
        show: false,
        minDate: '',
        showLXR: false,
        type: '',
        Serchvalue: '',
        allAmt: 0,
        fenzhu:true,
    },
    onLoad() {


        let that = this;
        var bean;
        const eventChannel = this.getOpenerEventChannel();
        eventChannel.on('acceptDataFromOpenerPage', function(data) {

            bean = data.type;

                that.setData({
                    showLXR: true,
                    type: bean,
                     goods: data.data,
                     fenzhu:data.fenzhu
                })
            
           

        })


        console.log(this.data.value);

    },
    onUnload() {

    },
    onShow: function() {
        let a = wx.getStorageSync('shuaxin');
        console.log(a);
        if (a != '11') {
            let that = this;
            Api.cheakGroupApi().then(res => {

                if (res.code == 0) {
                    var arr = res.data.groupInfo;
                    if (arr.length == 0) {

                        that.setData({
                            list: [],
                           


                        })


                    } else {
                        arr.forEach(ite => {
                            if (ite.recipientInfo != null) {
                                ite.recipientInfo.forEach(item => {
                                    item.skuNum = 1;
                                })

                            }
                        })
                        that.setData({
                            list: arr,

                        })
                    }




                } else {
                    that.setData({
                        list: [],


                    })
                }
            })
        }
        wx.setStorageSync('shuaxin', '');









        let dd = new Date()
        dd.setDate(dd.getDate() + 3)
        let time = dd.getTime()
        this.setData({
            minDate: time,
        })

    },
    addSum: function(e) {
        let i = e.currentTarget.dataset.inde;
        let j = e.currentTarget.dataset.index;
        var comSta = `list[${i}].recipientInfo[${j}].skuNum`;
        let a = this.data.list[i].recipientInfo[j].skuNum;


        this.setData({
            [comSta]: a + 1
        })
        let arg1 = codeZ.payAllSum(this.data.list,this.data.goods.goodsPrice);
       
        this.setData({
             allAmt:arg1                        
        })

    },
    jianSum: function(e) {
        let i = e.currentTarget.dataset.inde;
        let j = e.currentTarget.dataset.index;
        var comSta = `list[${i}].recipientInfo[${j}].skuNum`;
        let a = this.data.list[i].recipientInfo[j].skuNum;
        if (a == 1) {
            return

        }
        this.setData({
            [comSta]: a - 1
        })
        let arg1 = codeZ.payAllSum(this.data.list,this.data.goods.goodsPrice);
               
        this.setData({
             allAmt:arg1                        
        })

    },

    clickDe: function(e) {

        let i = e.currentTarget.dataset.inde;
        let j = e.currentTarget.dataset.index;
        var comSta = `list[${i}].recipientInfo[${j}].groupState`;
        let a = this.data.list[i].recipientInfo[j].groupState;
        if (a == null) {
            this.setData({
                [comSta]: '1'
            })
        } else {
            this.setData({
                [comSta]: null
            })
        }
        console.log(this.data.list);
        let arg1 = codeZ.payAllSum(this.data.list,this.data.goods.goodsPrice);
               
        this.setData({
             allAmt:arg1                        
        })
        // wx.navigateTo({
        //     url: '../shouDaoDetail/index',
        // })
    },
    addFu: function() {
        wx.navigateTo({
            url: '../Detail/index',
        })
    },
    jumpOrder: function(e) {

        let that = this;
        var listA = this.data.list;
        
        var cont = []
        if(listA.length == 0){
              return wx.showToast({
                    title: '请添加收货人',
                    icon: 'none'
                });
        }
        if (listA.length > 0) {

            listA.forEach(ite => {
                if (ite.recipientInfo != null) {
                    ite.recipientInfo.forEach(item => {
                        if (item.groupState == null) {
                            var a = {
                                'contactId': item['id'],
                                'skuNum': item['skuNum']
                            }
                            cont.push(a);
                        }
                    })

                }
            })



            if (cont.length == 0) {

                return wx.showToast({
                    title: '请添加收货人',
                    icon: 'none'
                });
            }

       
            let zhi = wx.getStorageSync('openId');
            let baseInfo = wx.getStorageSync('baseInfo');
          
            let data = {
                "cartAddContactDtoList": cont,
                "distributionTime": "",
                "goodsNo": this.data.goods.goodsNo,
                "openId": zhi,
                "shopkeeperCkNo": baseInfo.ckNo,
                "skuNo": this.data.goods.skuNo,
            }

            Api.skOrderCreateApi(data).then(res => {
                if (res.code == 0) {
                    that.wxPay(res.weChatAppletOrder);
                } else {
                    wx.showToast({
                        title: resp.msg
                    })
                } 
            })




        }


    },

    wxPay: function(weChatAppletOrder) {

        var that = this;
        wx.requestPayment({
            timeStamp: weChatAppletOrder.timeStamp,
            nonceStr: weChatAppletOrder.nonceStr,
            package: weChatAppletOrder.pay_package,
            signType: weChatAppletOrder.signType,
            paySign: weChatAppletOrder.paySign,
            success: function(res) {

                wx.showToast({
                    title: '支付成功'
                })
                wx.navigateTo({
                    url: '../MyOrderNew/index',
                })
            }
        })
    },







    clickCall() {
        wx.makePhoneCall({
            phoneNumber: "028-68091639",
            success: function() {

            },
            fail: function() {

                // console.log('拨打失败')
            }
        })
    },
    onInput(event) {
        console.log(event);
        var a = codeZ.js_date_time(event.detail, 'Y-M-D');
        this.setData({
            currentDate: a,
            show: false
        });
    },
    cancel: function() {
        this.setData({
            show: false
        })
    },

    popTime: function() {
        this.setData({
            show: true
        })
    },
    jumpHome: function() {
        wx.switchTab({
            url: '../../pages/index/index'
        })
    },
    bianji: function() {
        let a  =this.data.fenzhu;
        wx.navigateTo({
            url: '../MyShouLiRen/index?item='+a
        })
    },
    bianji2: function() {
        let a  =this.data.fenzhu;
        wx.navigateTo({
            url: '../adressEdit/index?fenzhu='+a
        })
    },
    jianCart: function() {

    },
    addCart: function() {

    },
    
    ZhaoHuo:function(){
        wx.switchTab({
            url:'../GoodsLook/index'
        })
    },
    GOhOME() {
        wx.switchTab({
            url: '../index/index'
        })
    
        // wx.showToast({
        //     title: '请先登录再进行分享',
        //     icon: 'none'
        // });
    },










});
